{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4170abd4-5ae6-4178-aa52-46f7865e7121",
   "metadata": {
    "tags": []
   },
   "source": [
    "# 数组形状操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2ba20622-bd89-495e-98a2-0d9d2ded55c1",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "fang_array = np.arange(10) #左闭右开区间\n",
    "fang_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e61e501f-890f-4041-aae1-9f323c2f64fd",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(10,)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "43f43b13-828f-4531-a6cd-d1ad71d0b330",
   "metadata": {
    "tags": []
   },
   "source": [
    "## 修改形状"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "ab9f63ec-6d50-4fd8-8bd7-ec936c589283",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "fang_array.shape = (2,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "b6d08ff6-4088-4e53-8b1e-f92d297d66dd",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3, 4],\n",
       "       [5, 6, 7, 8, 9]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "aa03ba42-3bcb-4860-8896-1922498d6c26",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array.reshape(1,10) #不会真的改变数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "326b45ed-7c9b-4200-a0ea-2d93df7cefab",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3, 4],\n",
       "       [5, 6, 7, 8, 9]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "24d15ef4-f672-4a3a-b778-d122f7e5281b",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array = np.arange(10)\n",
    "fang_array = fang_array[np.newaxis, :]\n",
    "fang_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "57e928ae-c93e-4781-9189-dd9e1c51c169",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 10)"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1b2744d0-675e-40f2-ba5c-ee0f5edd6e29",
   "metadata": {},
   "source": [
    "## 增加维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "4ca1155b-0ca5-468b-8fdf-a26d7bb43552",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0],\n",
       "       [1],\n",
       "       [2],\n",
       "       [3],\n",
       "       [4],\n",
       "       [5],\n",
       "       [6],\n",
       "       [7],\n",
       "       [8],\n",
       "       [9]])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array = np.arange(10)\n",
    "fang_array = fang_array[:,np.newaxis]\n",
    "fang_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "1a1f7c65-6ea0-4fca-9f8d-97fb9bb0f305",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[0]],\n",
       "\n",
       "       [[1]],\n",
       "\n",
       "       [[2]],\n",
       "\n",
       "       [[3]],\n",
       "\n",
       "       [[4]],\n",
       "\n",
       "       [[5]],\n",
       "\n",
       "       [[6]],\n",
       "\n",
       "       [[7]],\n",
       "\n",
       "       [[8]],\n",
       "\n",
       "       [[9]]])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array = np.arange(10)\n",
    "fang_array = fang_array[:, np.newaxis, np.newaxis]\n",
    "fang_array"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "38262f9c-1a8e-4e94-a7b9-9845ec1a25a2",
   "metadata": {},
   "source": [
    "删除空轴"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "a62519cd-b35c-4520-9633-b8d35b82dc2e",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array = fang_array.squeeze()\n",
    "fang_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "294adf64-1b89-4674-930a-362bced8d3b4",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3, 4],\n",
       "       [5, 6, 7, 8, 9]])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array.shape = (2,5)\n",
    "fang_array"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "808c605d-6aec-451e-a0df-edf7eb42b239",
   "metadata": {},
   "source": [
    "矩阵的转置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "7ae19ec7-1eba-4770-b425-38238a016388",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 5],\n",
       "       [1, 6],\n",
       "       [2, 7],\n",
       "       [3, 8],\n",
       "       [4, 9]])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array.transpose()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "5f9731df-beea-40e6-83d2-089b5dbfce8b",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 5],\n",
       "       [1, 6],\n",
       "       [2, 7],\n",
       "       [3, 8],\n",
       "       [4, 9]])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "afcbbaf7-a858-4f7e-8927-f715e040c830",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3, 4],\n",
       "       [5, 6, 7, 8, 9]])"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "32b36023-db21-4a3a-ae23-28442e46c6e7",
   "metadata": {},
   "source": [
    "## 数组的连接"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "ce3c8ffa-2b75-475b-8ece-79cfe03918f1",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 123,  456,  789],\n",
       "       [3214,  532,  213]])"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[123,456,789],[3214,532,213]])\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "bdab8a6d-3366-4282-b052-3786528b4975",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[123, 345, 342],\n",
       "       [ 23,  43, 213]])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.array([[123,345,342],[23,43,213]])\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "ccb45d72-cc07-4743-96e7-c7989502b08a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 123,  456,  789],\n",
       "       [3214,  532,  213],\n",
       "       [ 123,  345,  342],\n",
       "       [  23,   43,  213]])"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = np.concatenate((a,b))\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "a6f4a017-4957-4a7c-b022-dc760d348557",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 123,  456,  789,  123,  345,  342],\n",
       "       [3214,  532,  213,   23,   43,  213]])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = np.concatenate((a,b),axis = 1)\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "92bd2296-bc19-43bc-b9cf-069402823e32",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 6)"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "5b03cfc2-bd76-46fb-b466-77bd2f074167",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 123,  456,  789],\n",
       "       [3214,  532,  213],\n",
       "       [ 123,  345,  342],\n",
       "       [  23,   43,  213]])"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.vstack((a,b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "d6674a8c-2461-457e-84ad-3496fdb35dfa",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 123,  456,  789,  123,  345,  342],\n",
       "       [3214,  532,  213,   23,   43,  213]])"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.hstack((a,b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "e5df3971-1065-4f8f-bd9d-ab620ff1ca52",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 123,  456,  789],\n",
       "       [3214,  532,  213]])"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "fe5ab7da-d265-4126-9e6d-29a0a70270aa",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 123,  456,  789, 3214,  532,  213])"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.flatten()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "24c8941e-e452-47ec-b32d-01c38c13341d",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 123,  456,  789],\n",
       "       [3214,  532,  213]])"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "dffd8474-be7b-46c7-b12f-7f800e1d3886",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 123,  456,  789, 3214,  532,  213])"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.ravel()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
